package com.example.edu.config;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @author yanzhao
 * @date 2022/1/16 13:46
 * @since 1.0.0
 */
@Component
public class LocaleService {

    @Resource
    private MessageSource messageSource;

    public String getMessage(String code) {
        Locale locale = LocaleContextHolder.getLocale();
        String message = messageSource.getMessage(code, new Object[]{}, locale);
        if (message != null) {
            return message;
        }
        return code;
    }
}
